package zyz.framework.util;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by wp on 2017/1/20.
 */
public class ColumnUtils {
    /**
     * 下划线转驼峰法
     *
     * @param line       源字符串
     * @param smallCamel 大小驼峰,是否为小驼峰
     * @return 转换后的字符串
     */
    public static String underline2Camel(String line, boolean smallCamel) {
        if (line == null || "".equals(line)) {
            return "";
        }
        StringBuffer sb = new StringBuffer();
        Pattern pattern = Pattern.compile("([A-Za-z\\d]+)(_)?");
        Matcher matcher = pattern.matcher(line);
        while (matcher.find()) {
            String word = matcher.group();
            sb.append(smallCamel && matcher.start() == 0 ? Character.toLowerCase(word.charAt(0)) : Character.toUpperCase(word.charAt(0)));
            int index = word.lastIndexOf('_');
            if (index > 0) {
                sb.append(word.substring(1, index).toLowerCase());
            } else {
                sb.append(word.substring(1).toLowerCase());
            }
        }
        return sb.toString();
    }

    /**
     * 驼峰法转下划线 大写
     *
     * @param line 源字符串
     * @return 转换后的字符串
     */
    public static String camel2Underline(String line) {
        if (line == null || "".equals(line)) {
            return "";
        }
        line = String.valueOf(line.charAt(0)).toUpperCase().concat(line.substring(1));
        StringBuffer sb = new StringBuffer();
        Pattern pattern = Pattern.compile("[A-Z]([a-z\\d]+)?");
        Matcher matcher = pattern.matcher(line);
        while (matcher.find()) {
            String word = matcher.group();
            sb.append(word.toUpperCase());
            sb.append(matcher.end() == line.length() ? "" : "_");
        }
        return sb.toString();
    }

    /**
     * 驼峰法转下划线 小写
     *
     * @param line 源字符串
     * @return 转换后的字符串
     */
    public static String camel2LowUnderline(String line) {
        if (line == null || "".equals(line)) {
            return "";
        }
        line = String.valueOf(line.charAt(0)).toUpperCase().concat(line.substring(1));
        StringBuffer sb = new StringBuffer();
        Pattern pattern = Pattern.compile("[A-Z]([a-z\\d]+)?");
        Matcher matcher = pattern.matcher(line);
        while (matcher.find()) {
            String word = matcher.group();
            sb.append(word.toUpperCase());
            sb.append(matcher.end() == line.length() ? "" : "_");
        }
        return sb.toString().toLowerCase();
    }

    /**
     * 下划线转驼峰法
     *
     * @param data 源列表
     * @return 转换后列表
     */
    public static List<Map<String, Object>> keyConvert(List<Map<String, Object>> data) {
        List<Map<String, Object>> newData = new ArrayList<Map<String, Object>>();
        List<Map<String, Object>> oldData = data == null ? new ArrayList<Map<String, Object>>() : (List) data;
        for (Map<String, Object> temp : oldData) {
            newData.add(keyConvert(temp));
        }
        return newData;
    }

    /**
     * 下划线转驼峰法
     *
     * @param data 源MAP
     * @return 转换后MAP
     */
    public static Map<String, Object> keyConvert(Map<String, Object> data) {
        if (data == null || data.size() <= 0) {
            return null;
        }

        Map<String, Object> newData = new HashMap<String, Object>();
        for (String key : data.keySet()) {
            newData.put(ColumnUtils.underline2Camel(key, true), data.get(key));
        }

        return newData;
    }

    /**
     * Map驼峰转下划线
     *
     * @param data
     * @return
     */
    public static Map<String, Object> camel2UnderlineMap(Map<String, Object> data) {
        Set<String> keys = data.keySet();
        Map<String, Object> tempMap = new HashMap<String, Object>();
        for (String key : keys) {
            tempMap.put(camel2LowUnderline(key), data.get(key));
        }
        return tempMap;
    }

    public static void main(String[] args) {
        String line = "I_HAVE_aN_IPAbG3_PIG";
        String camel = underline2Camel(line, true);
        System.out.println(camel);
        System.out.println(camel2Underline(camel));
    }
}
